package com.food.restaurantpro.adapters;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.fragments.MenuPreOrderFragment;
import com.food.restaurantpro.imageloader.FileCache;
import com.food.restaurantpro.log.Log;
import com.food.restaurantpro.objects.Food;
import com.food.restaurantpro.utils.AlertDialogInterface;
import com.food.restaurantpro.utils.AlertDialogManager;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuPreOrderListViewAdapter extends ArrayAdapter<Food> {

	private MenuPreOrderFragment mMenuPreOrderFragment;
	private ArrayList<Food> mFoodList = new ArrayList<Food>();
	private FileCache m_FileCache;
	private LayoutInflater inflater;

	private int mTotalPayment;
	private TextView mTvTotalPayment;

	public MenuPreOrderListViewAdapter(MenuPreOrderFragment context,
			ArrayList<Food> foodList, TextView tvTotalPayment) {
		super(context.getActivity(), R.layout.menu_pre_order_lv_item, foodList);
		// TODO Auto-generated constructor stub
		mMenuPreOrderFragment = context;
		mFoodList = foodList;
		mTvTotalPayment = tvTotalPayment;
		m_FileCache = new FileCache(RestaurantApp.CURRENT_CONTEXT);
		inflater = LayoutInflater.from(mMenuPreOrderFragment.getActivity());

		mTotalPayment = 0;
		for (int i = 0; i < mFoodList.size(); i++) {
			if(mFoodList.get(i).getFoodAmount() == 0) {
				mFoodList.get(i).setFoodAmount(1);
			}
			try {
				int price = Integer.parseInt(mFoodList.get(i).getFoodPrice()
						.trim());
				mTotalPayment = mTotalPayment
						+ (mFoodList.get(i).getFoodAmount() * price);
			} catch (Exception e) {
				Log.e("MenuPreOrderListViewAdapter",
						"MenuPreOrderListViewAdapter() ", e);
			}
		}
		mTvTotalPayment.setText("Total: " + "$" + mTotalPayment);
	}

	@Override
	public int getCount() {
		return mFoodList.size();
	}

	@Override
	public Food getItem(int position) {
		return mFoodList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View listView = convertView;
		FoodHolder holder = null;
		if (convertView == null) {
			holder = new FoodHolder();
			listView = inflater.inflate(R.layout.menu_pre_order_lv_item, null);
			holder.imageView = (ImageView) listView
					.findViewById(R.id.imageview);
			holder.tvNamePreOder = (TextView) listView
					.findViewById(R.id.tvNamePreOder);
			holder.tvQuantity = (TextView) listView
					.findViewById(R.id.tvQuantity);
			holder.tvPrice = (TextView) listView
					.findViewById(R.id.tvCostPreOder);
			holder.btnPlus = (Button) listView
					.findViewById(R.id.btnPlusPreOder);
			holder.btnMinus = (Button) listView
					.findViewById(R.id.btnMinusPreOder);
			holder.btnPlus.setTag(position);
			holder.btnMinus.setTag(position);
			listView.setTag(holder);

		} else {
			listView = convertView;
			holder = (FoodHolder) listView.getTag();
		}
		final Food food = mFoodList.get(position);
		holder.imageView.setImageBitmap(m_FileCache.getBitmapByUrl(food
				.getFoodImage()));
		holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);

		holder.tvNamePreOder.setText(food.getFoodName());
		holder.tvQuantity.setText(mMenuPreOrderFragment.getText(R.string.quantity) + ""
				+ food.getFoodAmount());
		holder.tvPrice.setText("$" + food.getFoodPrice());

		final FoodHolder viewHolder1 = holder;
		holder.btnPlus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Object obj = v.getTag();
				final int which = ((Integer) obj).intValue();
				if (which > -1) {
					mFoodList.get(which).setFoodAmount(
							mFoodList.get(which).getFoodAmount() + 1);
					mMenuPreOrderFragment.getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							viewHolder1.tvQuantity.setText((mMenuPreOrderFragment
									.getText(R.string.quantity) + "" + mFoodList
									.get(which).getFoodAmount()));
							mTotalPayment = 0;
							for (int i = 0; i < mFoodList.size(); i++) {
								try {
									int price = Integer.parseInt(mFoodList
											.get(i).getFoodPrice().trim());
									mTotalPayment = mTotalPayment
											+ (mFoodList.get(i).getFoodAmount() * price);
								} catch (Exception e) {
									Log.e("MenuPreOrderListViewAdapter",
											"getView() ", e);
								}
							}
							mTvTotalPayment.setText("Total: " + "$"
									+ mTotalPayment);
						}
					});
				}
			}
		});

		holder.btnMinus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Object obj = v.getTag();
				final int which = ((Integer) obj).intValue();
				if (which > -1) {
					if (mFoodList.get(which).getFoodAmount() > 0) {
						mFoodList.get(which).setFoodAmount(
								mFoodList.get(which).getFoodAmount() - 1);
						mMenuPreOrderFragment.getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								viewHolder1.tvQuantity.setText((mMenuPreOrderFragment
										.getText(R.string.quantity) + "" + mFoodList
										.get(which).getFoodAmount()));
								mTotalPayment = 0;
								for (int i = 0; i < mFoodList.size(); i++) {
									try {
										int price = Integer.parseInt(mFoodList
												.get(i).getFoodPrice().trim());
										mTotalPayment = mTotalPayment
												+ (mFoodList.get(i)
														.getFoodAmount() * price);
									} catch (Exception e) {
										Log.e("MenuPreOrderListViewAdapter",
												"getView() ", e);
									}
								}
								mTvTotalPayment.setText("Total: " + "$"
										+ mTotalPayment);
							}
						});
						if (mFoodList.get(which).getFoodAmount() == 0) {
							goToRemoveItem(which);
						}
					} else if (mFoodList.get(which).getFoodAmount() == 0) {
						goToRemoveItem(which);
					}
				}
			}
		});
		return listView;
	}

	private void goToRemoveItem(final int index) {
		AlertDialogManager.showCustomDialog(mMenuPreOrderFragment.getActivity(),
				new AlertDialogInterface() {

					@Override
					public void onChooseYesCallback() {
						// TODO Auto-generated method stub
						mMenuPreOrderFragment.removeItemOfListView(index);
					}

					@Override
					public void onChooseNoCallback() {
						// TODO Auto-generated method stub

					}
				}, mMenuPreOrderFragment.getString(R.string.title_remove_dish),
				mMenuPreOrderFragment.getString(R.string.content_remove_dish),
				mMenuPreOrderFragment.getString(R.string.yes),
				mMenuPreOrderFragment.getString(R.string.no), true);
	}

	public int getTotalPayment() {
		return mTotalPayment;
	}
	//
	private static class FoodHolder {
		public ImageView imageView;
		public TextView tvNamePreOder;
		public TextView tvQuantity;
		public TextView tvPrice;
		public Button btnPlus;
		public Button btnMinus;
	}

	public void clearMemoryCache() {
		if (mFoodList != null) {
			mFoodList.clear();
			mFoodList = null;
		}
		m_FileCache = null;
		inflater = null;
	}

}
